﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO.Ports;
using System.IO;
using System.Windows.Forms;
using System.Threading;

namespace weiqijiance.灯光仪
{
    public class jiaoling6A : libass
    {
        public override bool openCOM(string sSet)
        {
            bool breturn = true;
            try
            {
                base.openCOM(sSet);
                comLI.ReadTimeout = 500;

                comLI.DataReceived += comLI_DataReceived;
            }
            catch
            {
                breturn = false;
            }
            return breturn;
        }



        public override bool sendLIcom(int cmd)//发送指令给灯光仪串口
        {
            bool flag = true;

            try
            {
                switch (cmd)
                {
                    case 1: { sendcmd("0x02 0x01 0x32 0x53 0x80 0xF8"); break; }       // 1:左靠位命令;
                    case 2: { sendcmd("0x02 0x01 0x32 0x53 0x81 0xF7"); break; }         // 2:右靠位命令;
                    case 3: { sendcmd("0x02 0x01 0x32 0x53 0x32 0x46"); break; }     // 3:二灯制;
                    case 4: { sendcmd("0x02 0x01 0x32 0x53 0x31 0x47"); break; }         // 4:四灯制;
                    case 5: { sendcmd("0x02 0x01 0x32 0x53 0x33 0x45"); break; }         // 5:单检测远光;
                    case 6: { sendcmd("0x02 0x01 0x32 0x53 0x34 0x44"); break; }     // 6:单检近光;
                    case 7: { sendcmd("0x02 0x01 0x32 0x53 0x35 0x43"); break; }     // 7:远近光均检;
                    case 8: { sendcmd("0x02 0x01 0x32 0x53 0x71 0x07"); break; }     // 8:先测远灯，后测近灯;
                    case 9: { sendcmd("0x02 0x01 0x32 0x54 0x07 0x70"); break; }         // 9:测量左灯;
                    case 10: { sendcmd("0x02 0x01 0x32 0x54 0x40 0x37"); break; }        // 10:测量右灯;

                    case 11: { sendcmd("0x02 0x01 0x32 0x47 0x4C 0x38"); break; }        // 11:取左主灯数据;
                    case 12: { sendcmd("0x02 0x01 0x32 0x47 0x4D 0x37"); break; }        // 12:取右主灯数据;

                    case 13: { sendcmd("0x02 0x01 0x32 0x4D 0x47 0x37"); break; }        // 13:仪器归位;


                }

            }
            catch
            {
                flag = false;
            }
            return flag;
        }
        public bool sendcmd(string sneirong)//发送指令给灯光仪串口
        {
            bool flag = true;
            try
            {
                string hexValues = sneirong;
                string[] hexValuesSplit = hexValues.Split(' ');
                int icount = 100;
                icount = hexValuesSplit.Length;
                if (comLI.IsOpen)
                {
                    Byte[] m_byte = new Byte[icount];
                    for (int i = 0; i < icount; i++)
                    {
                        m_byte[i] = (byte)Convert.ToInt32(hexValuesSplit[i], 16);
                    }
                    comLI.Write(m_byte, 0, icount);
                }
            }
            catch
            {
                flag = false;
            }
            return flag;
        }
        //灯光 返回数据
        void comLI_DataReceived(object sender, SerialDataReceivedEventArgs e)
        {

            string ygzyfh = "+";
            string ygsxfh = "+";
            string jgzyfh = "+";
            string jgsxfh = "+";
            if (comLI.IsOpen)
            {
                System.Threading.Thread.Sleep(300);
                int n = comLI.BytesToRead;//
                byte[] buf = new byte[n];
                comLI.Read(buf, 0, n);

                if (buf.Length > 0)
                {
                    StringBuilder strBuider1 = new StringBuilder();
                    strBuider1.Append(DateTime.Now.ToString("HH:mm:ss.fff") + ":");
                    for (int index = 0; index < buf.Length; index++)
                    {
                        strBuider1.Append(((int)buf[index]).ToString("X2") + " ");
                    }
                    Write(string.Format("./{0}信号.txt", "灯光6A"), strBuider1.ToString() + "\r\n");
                    //baocunRJ("信号板接受指令", strBuider1.ToString());
                }
                if (n > 19)//取数
                {
                    if (n < 22)//副灯
                    {
                        for (int i = 0; i < 1; i++)
                        {
                            if (buf[2 + i] == 0x2D)
                            {
                                ygzyfh = "-";
                            }
                            else
                            {
                                ygzyfh = "";
                            }
                            if (buf[7 + i] == 0x2D)
                            {
                                ygsxfh = "-";
                            }
                            else
                            {
                                ygsxfh = "";
                            }
                            int.TryParse(string.Format("{0}", (((buf[12 + i] - 48) * 1000 + (buf[13 + i] - 48) * 100 + (buf[14 + i] - 48) * 10 + (buf[15 + i] - 48)) * 100)), out iLIGQ);//远光强
                            float.TryParse(string.Format("{0}{1}", ygzyfh, ((buf[3 + i] - 48) * 100 + (buf[4 + i] - 48) * 10 + (buf[5 + i] - 48))), out fLIZYPY);//远光左右
                            float.TryParse(string.Format("{0}{1}", ygsxfh, ((buf[8 + i] - 48) * 100 + (buf[9 + i] - 48) * 10 + (buf[10 + i] - 48))), out fLISXPY);//远光上下
                            int.TryParse(string.Format("{0}", ((buf[16 + i] - 48) * 1000 + (buf[17 + i] - 48) * 100 + (buf[18 + i] - 48) * 10 + (buf[19 + i] - 48))), out iLIDG);//远光灯高
                            bqushu = true;
                        }

                    }
                    else//主灯
                    {
                        //for (int i = 0; i < 1; i++)
                        if (buf[0] == 2 && (buf[1] == 82 || buf[1] == 76))
                        {
                            if (buf[2] == 0x2D)
                            {
                                ygzyfh = "-";
                            }
                            else
                            {
                                ygzyfh = "";
                            }
                            if (buf[7] == 0x2D)
                            {
                                ygsxfh = "-";
                            }
                            else
                            {
                                ygsxfh = "";
                            }
                            if (buf[20] == 0x2D)
                            {
                                jgzyfh = "-";
                            }
                            else
                            {
                                jgzyfh = "";
                            }
                            if (buf[25] == 0x2D)
                            {
                                jgsxfh = "-";
                            }
                            else
                            {
                                jgsxfh = "";
                            }
                            int.TryParse(string.Format("{0}", (((buf[12] - 48) * 1000 + (buf[13] - 48) * 100 + (buf[14] - 48) * 10 + (buf[15] - 48)) * 100)), out iLIGQ);//远光强
                            float.TryParse(string.Format("{0}{1}", ygzyfh, ((buf[3] - 48) * 100 + (buf[4] - 48) * 10 + (buf[5] - 48))), out fLIZYPY);//远光左右
                            float.TryParse(string.Format("{0}{1}", ygsxfh, ((buf[8] - 48) * 100 + (buf[9] - 48) * 10 + (buf[10] - 48))), out fLISXPY);//远光上下
                            int.TryParse(string.Format("{0}", ((buf[16] - 48) * 1000 + (buf[17] - 48) * 100 + (buf[18] - 48) * 10 + (buf[19] - 48))), out iLIDG);//远光灯高
                            int.TryParse(string.Format("{0}", (((buf[30] - 48) * 1000 + (buf[31] - 48) * 100 + (buf[32] - 48) * 10 + (buf[33] - 48)) * 100)), out iLIGQ2);//左近光光强
                            float.TryParse(string.Format("{0}{1}", jgzyfh, ((buf[21] - 48) * 100 + (buf[22] - 48) * 10 + (buf[23] - 48))), out fLIZYPY2);//近光左右
                            float.TryParse(string.Format("{0}{1}", jgsxfh, ((buf[26] - 48) * 100 + (buf[27] - 48) * 10 + (buf[28] - 48))), out fLISXPY2);//近光上下
                            int.TryParse(string.Format("{0}", ((buf[34] - 48) * 1000 + (buf[35] - 48) * 100 + (buf[36] - 48) * 10 + (buf[37] - 48))), out iLIDG2);//近光高
                            bqushu = true;
                        }
                    }
                }
                else
                {
                    for (int i = 0; i < 3; i++)
                    {
                        if (i >= buf.Length - 3)
                            break;
                        if (buf[0 + i] == 0x02 && buf[1 + i] == 0x32 && buf[2 + i] == 0x54 && buf[3 + i] == 0x40)//e2
                        {
                            //bguiwei = true;
                        }
                        if (buf[0 + i] == 0x02 && buf[1 + i] == 0x85 && buf[2 + i] == 0x02)//接受仪器返回
                        {
                            if ((buf[5 + i] == 0x32 && buf[6 + i] == 0x06) || (buf[5 + i] == 0x35 && buf[6 + i] == 0x09))//主远测量结束
                            {
                                byuanguang = true;

                            }
                            //else if (buf[3 + i] == 0x4E)//副远测量结束
                            //{
                            //    //byuanguangfudeng = true;


                            //}
                            else if ((buf[5 + i] == 0x37 && buf[6 + i] == 0x0B) || (buf[5 + i] == 0x39 && buf[6 + i] == 0x0D))//近光灯测量结束
                            {
                                bjingguang = true;
                                if (!byuanguang)
                                    bjcshibai = true;
                                byuanguang = true;

                            }


                        }
                        if (buf[0 + i] == 0x02 && buf[1 + i] == 0x32 && buf[2 + i] == 0x06)
                        {
                            if (buf[3 + i] == 0x72)//退出测量
                            {
                                btuichu = true;
                            }
                        }
                        if (buf[0 + i] == 0x02 && buf[1 + i] == 0x32 && buf[2 + i] == 0x45 && buf[3 + i] == 0x13)//e2
                        {
                            bjcshibai = true;
                        }
                        //02  32  54  40  38
                    }

                }
            }

        }
    }
}
